﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.IO;

public partial class Admin_ucControl_ucDowload : System.Web.UI.UserControl
{
    TblDowloadDAO dowloadDAO = new TblDowloadDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_ADMIN_USER] == "" || Session[Global.SESSION_ADMIN_USER] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            string id = Request.QueryString["id"];
            if (id != null)
            {
                LoadDSDowload();
                LoadDetail();
            }
            else
            {
                LoadDSDowload();
            }
        }
    }

    private void LoadDetail()
    {
        string id = Request.QueryString["id"];
        TblDowload dowload = dowloadDAO.GetDowloadById(Global.ConvertToInt(id));
        txttenungdung.Text = dowload.NameFile;


    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];

        bool flag = false;
        try
        {

            TblDowload dowload = new TblDowload();

            dowload.NameFile = txttenungdung.Text;


            if (id == null || id == "")
            {
                dowload.ID  = 0;
                string path = Server.MapPath("~\\Admin\\images\\Items\\");
                string filename = uploadFile.FileName;

                // lblMessage.Text = path;
                if (File.Exists(path + "\\" + filename))
                {
                    lbMessage.Text = "File này đã tồn tại.Hãy chọn một file khác";
                    return;
                }
                uploadFile.SaveAs(path + "\\" + filename);
                dowload.URLDowload = filename;

                dowloadDAO.InsDowload(dowload, 1);
            }
            else
            {
                string path = Server.MapPath("~\\Admin\\images\\Items\\");
                string filename = uploadFile.FileName;

                // lblMessage.Text = path;
                if (File.Exists(path + "\\" + filename))
                {
                    lbMessage.Text = "File này đã tồn tại.Hãy chọn một file khác";
                    return;
                }
                uploadFile.SaveAs(path + "\\" + filename);
                dowload.URLDowload = filename;
                dowload.ID = Global.ConvertToInt(id);
                dowloadDAO.InsDowload(dowload, 2);            
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            LoadDSDowload();

        }
    }

    private void LoadDSDowload()
    {
        grdowload.DataSource = dowloadDAO.GetDSdowload();
        grdowload.DataBind();
    }
  
    protected void grdowload_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    ImageButton button = control as ImageButton;
                    if (button != null && button.CommandName == "Delete")
                    {
                        // Add delete confirmation
                        button.CommandArgument = ((TblItemImage)e.Row.DataItem).ID.ToString();
                        button.OnClientClick = "if (!confirm('Bạn có thật sự muốn xóa không ?')) return false;";
                    }
                }
            }
        }
    }

     protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = true;
        try
        {
            TblDowload dowload = new TblDowload();

            dowload.ID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);

            dowload = dowloadDAO.GetDowloadById(idXoa);

            string filename = dowload.URLDowload;

            dowloadDAO.DeleteDowLoad(dowload);

            if (File.Exists(Server.MapPath("~\\Admin\\images\\Items\\" + filename)))
            {
                File.Delete(Server.MapPath("~\\Admin\\images\\Items\\" + filename));
            }

            flag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }

        if (flag)
        {
            LoadDSDowload();
        }
    }
   
}
